package action.ManagerAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.CroBuycars;
import entity.CroCustomer;
import entity.CroPurchase;
import entity.CroReturngoods;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroPurgoodsService;
import service.CroReturngoodsService;
import vo.CroReturngoodsVo;

import java.util.List;

/**
 * Created by wsx on 2017/5/11.
 */
public class ReturngoodsAction extends ActionSupport {
    @Autowired
    private CroReturngoodsService croReturngoodsService;

    private List<CroReturngoods> croReturngoodsList;
    private CroReturngoods croReturngoods;
    private CroReturngoodsVo croReturngoodsVo;
    private CroReturngoodsVo recorCroReturngoodsVo;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 10;
    private String jsonObject;
    private String id;
    private String successResult;
    private Double totalprice;
    private String yesorno;

    /*
    * 所有退货
    * */
    @RequiresPermissions("user:select")
    public String returnGoods(){
        Pagination<CroReturngoods> page;
        if(pageIndex==0){
            page=croReturngoodsService.findAllReturngoods(1,pageSize);
        }else{
            page=croReturngoodsService.findAllReturngoods(pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setCroReturngoodsList(page.getItems());
        return SUCCESS;
    }
        /*
    * 按条件找出退货
    * */
        @RequiresPermissions("user:select")
    public String findReturnGoodsByCondiction(){
            System.out.println("findReturnGoodsAction "+"username:"+croReturngoodsVo.getUsername()+
                    "status:"+croReturngoodsVo.getReturnstatus()+"id:"+croReturngoodsVo.getUsername());
            Pagination<CroReturngoods> page;
            if(pageIndex==0){
                page=croReturngoodsService.findReturngoodsByConditction(1,pageSize,croReturngoodsVo);
            }else{
                page=croReturngoodsService.findReturngoodsByConditction(pageIndex,pageSize,croReturngoodsVo);
            }
            setPageCount((int) page.getPagesCount());
            setPageIndex((int) page.getCurIndex());
            setCroReturngoodsList(page.getItems());
            setRecorCroReturngoodsVo(croReturngoodsVo);
            setJsonObject(JSON.toJSONString(croReturngoodsVo));
            return SUCCESS;
        }

        /*
        * 根据id删除退货申请
        * */
        @RequiresPermissions("user:delete")
    public String deleteReturngoods(){
            System.out.println("id："+id);
            if(croReturngoodsService.deleteReturngoods(id)){
                successResult = "删除成功！";
            }else{
                successResult = "删除失败！";
            }
            return SUCCESS;
        }

        /*
        * 获取退货信息详情
        * */
        @RequiresPermissions("user:select")
    public String getReturngoodsDetail(){
            System.out.println("getReturngoodsDetailAction;id:"+id);
            croReturngoods=croReturngoodsService.getReturngoodsDetail(id);
           totalprice=croReturngoods.getCroPurgoods().getUniprice()*Integer.parseInt(croReturngoods.getGoodsnumber());
            return SUCCESS;
        }

           /*
* 改变退货状态
* */
           @RequiresPermissions("user:update")
    public String changeReturngoodsStatus(){
        System.out.println("id:"+id+" yesorno"+yesorno);
        String status=null;
        if(!yesorno.equals("-1")) {
            if (yesorno.equals("0")) {
                System.out.println("---------");
                status = "2";
            } else {
                status = "1";
            }
        }
        if(croReturngoodsService.changeReturnGoodsStatus(id,status)) {
            return SUCCESS;
        }else{
            return null;
        }
    }



    public List<CroReturngoods> getCroReturngoodsList() {
        return croReturngoodsList;
    }

    public void setCroReturngoodsList(List<CroReturngoods> croReturngoodsList) {
        this.croReturngoodsList = croReturngoodsList;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public CroReturngoodsService getCroReturngoodsService() {
        return croReturngoodsService;
    }

    public void setCroReturngoodsService(CroReturngoodsService croReturngoodsService) {
        this.croReturngoodsService = croReturngoodsService;
    }

    public CroReturngoodsVo getCroReturngoodsVo() {
        return croReturngoodsVo;
    }

    public void setCroReturngoodsVo(CroReturngoodsVo croReturngoodsVo) {
        this.croReturngoodsVo = croReturngoodsVo;
    }

    public CroReturngoodsVo getRecorCroReturngoodsVo() {
        return recorCroReturngoodsVo;
    }

    public void setRecorCroReturngoodsVo(CroReturngoodsVo recorCroReturngoodsVo) {
        this.recorCroReturngoodsVo = recorCroReturngoodsVo;
    }

    public String getJsonObject() {
        return jsonObject;
    }

    public void setJsonObject(String jsonObject) {
        this.jsonObject = jsonObject;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public CroReturngoods getCroReturngoods() {
        return croReturngoods;
    }

    public void setCroReturngoods(CroReturngoods croReturngoods) {
        this.croReturngoods = croReturngoods;
    }

    public Double getTotalprice() {
        return totalprice;
    }

    public void setTotalprice(Double totalprice) {
        this.totalprice = totalprice;
    }

    public String getYesorno() {
        return yesorno;
    }

    public void setYesorno(String yesorno) {
        this.yesorno = yesorno;
    }
}
